package com.cyan.app.workflow.activity.TQM;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.cyan.app.barcode.CaptureActivity;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.activity.Common.MiscodeActivity;
import com.cyan.app.workflow.base.activity.BaseFragmentActivity;
import com.cyan.app.workflow.bean.CommonBean;
import com.cyan.app.workflow.bean.TqmpoolLabBean;
import com.cyan.app.workflow.common.HttpConfig;
import com.cyan.app.workflow.customview.ShowTipDialog;
import com.cyan.app.workflow.util.AppUtils;
import com.cyan.app.workflow.util.network.GetRequestParams;
import com.google.gson.Gson;

import org.json.JSONObject;

/**
 * Created by DXW on 2017-04-26.
 */

public class TqmMannoDetailActivity extends BaseFragmentActivity {

    private TqmpoolLabBean.TqmpoolBean mTqmpoolBean;
    private TqmpoolLabBean.TqmpoolBean rerultTqmpoolBean;
    private EditText mEditTextAccqty;
    private EditText mEditTextType;
    private EditText mEditTextUser;
    private EditText mEditTextQc;
    private EditText mEditTextOk;
    private EditText mEditTextNg;
    private EditText mEditTextBad;
    private EditText mEditTextRemarks;
    private CheckBox mCheckBoxSelected;
    private ShowTipDialog showdialog = new ShowTipDialog();
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tqm_manno_detail);
        titleBar();
        initView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0x0002 && resultCode == RESULT_OK){
            String result = "";
            if(data != null) {
                result = data.getStringExtra("result");
                mEditTextType.setText(String.valueOf(result));
            }
        }
        if(requestCode == 0x0003 && resultCode == RESULT_OK) {
            String result = "";
            if(data != null) {
                result = data.getStringExtra("result");
            }
            if (!result.contains("40010")) {
                AppUtils.showTip(TqmMannoDetailActivity.this, "员工不存在");
            }else {
                String pisUser = mEditTextUser.getText().toString();
                if(pisUser.contains(result.substring(5)))
                    AppUtils.showTip(TqmMannoDetailActivity.this, "员工已存在");
                else if(!pisUser.isEmpty())
                    pisUser = pisUser + "," + result.substring(5);
                else
                    pisUser =  result.substring(5);
                mEditTextUser.setText(pisUser);
            }
        }
    }

    private void titleBar() {
        TextView mTextView =(TextView)findViewById(R.id.top_title_info);
        mTextView.setText("质检确认");
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
    }
    private void initView() {
        mTqmpoolBean = (TqmpoolLabBean.TqmpoolBean) getIntent().getSerializableExtra("tqmMannoDetail");
        rerultTqmpoolBean = (TqmpoolLabBean.TqmpoolBean) getIntent().getSerializableExtra("tqmMannoDetail");
        View resultView =  findViewById(R.id.tqm_manno_detail_result_lay);
        if(mTqmpoolBean.getQctype()==2) {
            TextView mEditTextQc = (TextView) findViewById(R.id.tqm_manno_detail_result_qc);
            mEditTextQc.setText("抽检 " + mTqmpoolBean.getQcqty().toString());

            TextView mEditTextOk = (TextView) findViewById(R.id.tqm_manno_detail_result_ok);
            mEditTextOk.setText("良品 " +  (mTqmpoolBean.getQcqty()-mTqmpoolBean.getNgqty()-mTqmpoolBean.getBadqty()));

            TextView mEditTextNg = (TextView) findViewById(R.id.tqm_manno_detail_result_ng);
            mEditTextNg.setText("不良 " + mTqmpoolBean.getNgqty().toString());

            TextView mEditTextBad = (TextView) findViewById(R.id.tqm_manno_detail_result_bad);
            mEditTextBad.setText("报废 " + mTqmpoolBean.getBadqty().toString());
        }else {
            resultView.setVisibility(View.GONE);
        }

        mEditTextType = (EditText) findViewById(R.id.tqm_manno_detail_type);
        mEditTextAccqty = (EditText)findViewById(R.id.tqm_manno_detail_accqty);
        mEditTextAccqty.setText(mTqmpoolBean.getAccqty().toString());

        mEditTextQc = (EditText)findViewById(R.id.tqm_manno_detail_qcqty);
        mEditTextNg = (EditText)findViewById(R.id.tqm_manno_detail_ngqty);
        mEditTextNg.setText("0");
        mEditTextBad = (EditText)findViewById(R.id.tqm_manno_detail_badqty);
        mEditTextBad.setText("0");
        mEditTextRemarks = (EditText)findViewById(R.id.tqm_manno_detail_remarks);
        mCheckBoxSelected = (CheckBox) findViewById(R.id.tqm_manno_detail_selected);

        mEditTextUser = (EditText)findViewById(R.id.tqm_manno_detail_user) ;
        TextView clearUser = (TextView)findViewById(R.id.tqm_manno_detail_clearuser);
        clearUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEditTextUser.setText("");
            }
        });
        Button userButton=(Button)findViewById(R.id.whm_cxll_ll_pisuser_sm);
        userButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(TqmMannoDetailActivity.this, CaptureActivity.class), 0x0003);
            }
        });
        Button typeButton=(Button)findViewById(R.id.tqm_manno_detail_type_sm);
        typeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(TqmMannoDetailActivity.this, MiscodeActivity.class);
                intent.putExtra("miscode_ckind","1B");
                startActivityForResult(intent, 0x0002);
            }
        });

        mCheckBoxSelected.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!mCheckBoxSelected.isChecked()) return;

                if(mEditTextQc.getText().length()==0||Double.parseDouble(mEditTextQc.getText().toString())<0){
                    AppUtils.showTip(TqmMannoDetailActivity.this,"请输入抽检数量");
                    return ;
                }
                if(rerultTqmpoolBean.getAccqty()>Double.parseDouble(mEditTextQc.getText().toString())) {
                    AppUtils.showTip(TqmMannoDetailActivity.this, "注意:流转数=送检数 - 不良数 - 报废数");
                }
            }
        });
        Button submitButton=(Button)findViewById(R.id.tqm_manno_detail_submit);
        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] obj = buildResultObj();
                if(obj[0]=="0"){
                    AppUtils.showTip(TqmMannoDetailActivity.this,obj[1]);
                }else {
                    submitAsyncLoader task =  new submitAsyncLoader();
                    task.execute("602");
                }
            }
        });
    }
    private String[] buildResultObj(){
        String[] result = new String[]{"0",""};
        if(mEditTextQc.getText().length()==0||Double.parseDouble(mEditTextQc.getText().toString())<0){
            result[1]="抽检数量必须大于0";
            return result;
        }
        rerultTqmpoolBean.setQcqty(Double.parseDouble(mEditTextQc.getText().toString()));
        if(mEditTextNg.getText().length()==0||Double.parseDouble(mEditTextNg.getText().toString())<0){
            result[1]="不良数量必须大于等于0";
            return result;
        }
        rerultTqmpoolBean.setNgqty(Double.parseDouble(mEditTextNg.getText().toString()));

        if(mEditTextBad.getText().length()==0||Double.parseDouble(mEditTextBad.getText().toString())<0){
            result[1]="报废数量必须大于等于0";
            return result;
        }
        rerultTqmpoolBean.setBadqty(Double.parseDouble(mEditTextBad.getText().toString()));

        if(rerultTqmpoolBean.getBadqty()>0||rerultTqmpoolBean.getNgqty()>0){
            if(mEditTextType.getText().length()==0) {
                result[1] = "请选择不良或报废类型";
                return result;
            }
        }
        rerultTqmpoolBean.setNgtype(mEditTextType.getText().toString());
        rerultTqmpoolBean.setRemarks(mEditTextRemarks.getText().toString());

        rerultTqmpoolBean.setOkqty(rerultTqmpoolBean.getAccqty()-rerultTqmpoolBean.getBadqty()-rerultTqmpoolBean.getNgqty());
        rerultTqmpoolBean.setRemarks(mEditTextRemarks.getText().toString());

        if(rerultTqmpoolBean.getAccqty()<rerultTqmpoolBean.getQcqty()){
            result[1] = "抽检数量不能大于送检数量。";
            return result;
        }
        if((rerultTqmpoolBean.getNgqty()+rerultTqmpoolBean.getBadqty())>rerultTqmpoolBean.getQcqty()){
            result[1] = "不良报废数量不能大于抽检数量。";
            return result;
        }
        if(mEditTextUser.getText().length()==0) {
            result[1] = "检验人员不能为空";
            return result;
        }
        rerultTqmpoolBean.setQcuserno(mEditTextUser.getText().toString());
        if(mCheckBoxSelected.isChecked()) {
            rerultTqmpoolBean.setIsqc(1);
        }else {
            rerultTqmpoolBean.setIsqc(0);
        }
        result[0] = "1";
        return result;
    }
    public class submitAsyncLoader extends AsyncTask<String, Integer, String> {
        @Override
        protected void onPreExecute() {
            showdialog.showDialog(TqmMannoDetailActivity.this);
        }

        @Override
        protected String doInBackground(String... params) {
            String result = null;
            String action =params[0];
            try{
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("qcno", rerultTqmpoolBean.getQcno());
                jsonObject.put("qcqty", rerultTqmpoolBean.getQcqty());
                jsonObject.put("ngqty", rerultTqmpoolBean.getNgqty());
                jsonObject.put("badqty", rerultTqmpoolBean.getBadqty());
                jsonObject.put("ngtype", rerultTqmpoolBean.getNgtype());
                jsonObject.put("remarks", rerultTqmpoolBean.getRemarks());
                jsonObject.put("qcuserno", rerultTqmpoolBean.getQcuserno());
                //jsonObject.put("qcuserno", "1019");
                jsonObject.put("isqc", rerultTqmpoolBean.getIsqc());
                result= GetRequestParams.getRequestResult(HttpConfig.HTTP_MAIN_SERVER_URL, action, jsonObject.toString(), getApplicationContext());
            }catch (Exception e){
                Log.e("error", e.toString());
            }
            return result;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s == null||s.isEmpty()||s.contains("html")) {
                AppUtils.showTip(TqmMannoDetailActivity.this,"操作异常，请稍后再试！");
                showdialog.closeDialog();
                return;
            }
            CommonBean commonBean = new Gson().fromJson(s, CommonBean.class);
            if (commonBean.getCode()==1) {
                Intent intent = new Intent();
                intent.putExtra("qcno",rerultTqmpoolBean.getQcno());
                TqmMannoDetailActivity.this.setResult(RESULT_OK,intent);
                AppUtils.showTip(TqmMannoDetailActivity.this,"保存成功。");
                showdialog.closeDialog();
                finish();
            }else {
                TqmMannoDetailActivity.this.setResult(RESULT_CANCELED);
                AppUtils.showTip(TqmMannoDetailActivity.this,"保存失败，"+commonBean.getMsg());
                showdialog.closeDialog();
            }
        }

        @Override
        protected void onCancelled(String s) {
            TqmMannoDetailActivity.this.setResult(RESULT_CANCELED);
        }
    }
}
